package com.dbh.algorithm.future;

/**
 *  用来提取RealData的一个订单
 *
 * @author dongbanghui
 * @date 2018/06/25
 */
public class FutureData implements Data {

    /**
     * FutureData是RealData的包装
     */
    protected RealData realData = null;
    protected boolean isReady = false;
    public synchronized void setRealData(RealData realData){
        if(isReady){
            return;
        }
        this.realData = realData;
        isReady = true;
        //RealData已经被注入，通知getResult()
        notifyAll();
    }

    /**
     * 会等待RealData的完成
     * @return
     */
    @Override
    public synchronized String getResult() {
        while(!isReady){
            try {
                //一直等待，知道RealData被注入
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //由RealData来实现
        return realData.result;
    }

}
